<?php
$message = '';
$db = new mysqli('localhost', 'root', 'root', 'rashidStone');
if($db->connect_error) {
    $message = $db->connect_error;
} else {
   $sql = 'SELECT User.FirstName, User.LastName, User.UserID, Class.Name FROM User LEFT JOIN Class ON Class.ClassID = User.ClassID';
   $result = $db->query($sql);
   if($db->error) {
       $message = $db->error;
   }
}
?>

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title>View Student</title>
          <link rel="stylesheet" href="css/bootstrap.min.css">

    </head>
    <nav class="navbar navbar-default" role="navigation">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Rashid Stone</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Manage Students <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><a href="addStudent.html">Add Student</a></li>
            <li><a href="enroll.php">Enroll Student</a></li>
          </ul>
        </li>
          <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Manage Tags <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><a href="addTag.html">Add Tag</a></li>
            <li><a href="tagStudent.php">Tag Student</a></li>
          </ul>
        </li>
        <li><a href="addClass.html">Add Class</a></li>
        <li >
       <a href="viewStudents.php"> View Students</a>
        </li>
        <li class="active">
       <a href="viewTags.php"> View Tags</a>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

    <body>
       <div class="container"> <?php if ($message) {
         echo "<h2>$message</h2>";
         } else {
         ?>
         <table class="table table-striped">
             <thead>
                 <th>First Name</th>
                 <th>Last Name</th>
                 <th>Class</th>
                 <th>Tag</th>
             </thead>
         <?php while ($row = $result->fetch_assoc()) { ?>
         <tbody>
             <tr>
                 <td><?php echo $row['FirstName']?></td>
                 <td><?php echo $row['LastName']?></td>
         
                 <td><?php echo $row['Name']?></td>
                 <td><?php 
         
                 $sql = "SELECT TagAssignment.TagID FROM TagAssignment WHERE TagAssignment.UserID =  '$row[UserID]'";
                 $tag = $db->query($sql) or die($sql."<br/><br/>".mysql_error());;
                 while ($tagresult = $tag->fetch_assoc()) {
                  $tagresult = $tagresult['TagID'];

                  $sql ="SELECT Tag.Name FROM Tag WHERE Tag.TagID = '$tagresult'"; 
                  $name = $db->query($sql) or die($sql."<br/><br/>".mysql_error());
                  $name = $name->fetch_assoc();
                  $name = $name['Name'];
                  echo "<p> $name </p>";
                 }
         
                 ?></td>
         
             </tr>
             </tbody>
         <?php } //end of loop ?>
         </table>
         <?php } //end of page ?></div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript" src="js/script.js"></script>
    <script src="js/bootstrap.min.js"></script>
    </body>
</html>
